"""
系统管理基础数据初始化
"""
import os
import sys
sys.path.append("..")  # Linux上运行改py，必需增加此，才能正确引用utils
import django
from utils.BaseInitialize import BaseInitialize
from systems.models import UserProfile, Dept, Menu, MenuButton, Role, Dictionary

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
django.setup()


class Initialize(BaseInitialize):

    def init_dept(self):
        """
        初始化部门信息
        """
        self.dept_data = [
            {"id": "1", "creator": "admin", "modifier": "admin", "sort": 1, "parent_id": None, "name": "昜丿捺科技有限责任公司"},
            {"id": "2", "creator": "admin", "modifier": "admin", "sort": 1, "parent_id": "1", "name": "财务部"},
            {"id": "3", "creator": "admin", "modifier": "admin", "sort": 2, "parent_id": "1", "name": "销售部"}
        ]
        self.save(Dept, self.dept_data, "部门信息")

    def init_button(self):
        """
        初始化按钮
        """
        self.button_data = [
            {"id": "1", "code": "button", "label": "按钮", "value": "button", "sort": 1, "parent_id": None, "creator": "admin", "modifier": "admin"},
            {"id": "2", "code": "create", "label": "新增", "value": "create", "sort": 1, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
            {"id": "3", "code": "delete", "label": "删除", "value": "delete", "sort": 2, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
            {"id": "4", "code": "update", "label": "编辑", "value": "update", "sort": 3, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
            {"id": "5", "code": "search", "label": "查询", "value": "search", "sort": 4, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
            {"id": "6", "code": "save"  , "label": "保存", "value": "save"  , "sort": 5, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
            {"id": "7", "code": "export", "label": "导出", "value": "export", "sort": 5, "parent_id": "1" , "creator": "admin", "modifier": "admin"},
        ]
        self.save(Dictionary, self.button_data, "字典（按钮信息）")

    def init_menu(self):
        """
        初始化菜单表
        """
        self.menu_data = [
            {"id": "1", "name": "系统管理", "sort": 1, "parent_id": None,"creator": "admin", "modifier": "admin", "web_path": "", "icon": "mdi-account", "is_catalog": "1"},
            {"id": "2", "name": "部门管理", "sort": 1, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/dept/list/"},
            {"id": "3", "name": "菜单管理", "sort": 2, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/menu/list/"},
            {"id": "4", "name": "角色管理", "sort": 3, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/role/list/"},
            {"id": "5", "name": "权限管理", "sort": 4, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/per/list/"},
            {"id": "6", "name": "用户管理", "sort": 5, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/user/list/"},
            {"id": "7", "name": "字典管理", "sort": 6, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/dic/list/"},
            {"id": "8", "name": "操作日志", "sort": 7, "parent_id": "1", "creator": "admin", "modifier": "admin", "web_path": "/systems/log/list/"},
        ]
        self.save(Menu, self.menu_data, "菜单表")

    def init_menu_button(self):
        """
        初始化菜单按钮权限表
        """
        self.menu_button_data = [
            {"id": "1" , "creator": "admin", "modifier": "admin", "menu_id": "2", "name": "新增", "key": "create"},
            {"id": "2" , "creator": "admin", "modifier": "admin", "menu_id": "2", "name": "删除", "key": "delete"},
            {"id": "3" , "creator": "admin", "modifier": "admin", "menu_id": "2", "name": "编辑", "key": "update"},
            {"id": "4" , "creator": "admin", "modifier": "admin", "menu_id": "2", "name": "查询", "key": "search"},

            {"id": "5" , "creator": "admin", "modifier": "admin", "menu_id": "3", "name": "新增", "key": "create"},
            {"id": "6" , "creator": "admin", "modifier": "admin", "menu_id": "3", "name": "删除", "key": "delete"},
            {"id": "7" , "creator": "admin", "modifier": "admin", "menu_id": "3", "name": "编辑", "key": "update"},
            {"id": "8" , "creator": "admin", "modifier": "admin", "menu_id": "3", "name": "查询", "key": "search"},

            {"id": "9" , "creator": "admin", "modifier": "admin", "menu_id": "4", "name": "新增", "key": "create"},
            {"id": "10", "creator": "admin", "modifier": "admin", "menu_id": "4", "name": "删除", "key": "delete"},
            {"id": "11", "creator": "admin", "modifier": "admin", "menu_id": "4", "name": "编辑", "key": "update"},
            {"id": "12", "creator": "admin", "modifier": "admin", "menu_id": "4", "name": "查询", "key": "search"},

            {"id": "13", "creator": "admin", "modifier": "admin", "menu_id": "5", "name": "保存", "key": "save"},

            {"id": "14", "creator": "admin", "modifier": "admin", "menu_id": "6", "name": "新增", "key": "create"},
            {"id": "15", "creator": "admin", "modifier": "admin", "menu_id": "6", "name": "删除", "key": "delete"},
            {"id": "16", "creator": "admin", "modifier": "admin", "menu_id": "6", "name": "编辑", "key": "update"},
            {"id": "17", "creator": "admin", "modifier": "admin", "menu_id": "6", "name": "查询", "key": "search"},

            {"id": "18", "creator": "admin", "modifier": "admin", "menu_id": "7", "name": "新增", "key": "create"},
            {"id": "19", "creator": "admin", "modifier": "admin", "menu_id": "7", "name": "删除", "key": "delete"},
            {"id": "20", "creator": "admin", "modifier": "admin", "menu_id": "7", "name": "编辑", "key": "update"},
            {"id": "21", "creator": "admin", "modifier": "admin", "menu_id": "7", "name": "查询", "key": "search"},

            {"id": "22", "creator": "admin", "modifier": "admin", "menu_id": "8", "name": "查询", "key": "search"},
        ]
        self.save(MenuButton, self.menu_button_data, "菜单按钮权限表")

    def init_role(self):
        """
        初始化角色表
        """
        data = [
            {"id": "1", "creator": "admin", "modifier": "admin", "sort": 1, "is_admin": 1, "data_range": 3, "name": "管理员", "key": "admin",
             # "menu": [ele.get("id") for ele in self.menu_data],
             # "permission": [ele.get("id") for ele in self.menu_button_data]
             "menu": ["1", "2", "6", "9", "10", "11", "12", "13", "14", "15", "16", ],
             "permission": ["1 ", "2 ", "3 ", "4 ", "14", "15", "16", "17", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", ]
             },

            {"id": "2", "creator": "admin", "modifier": "admin", "sort": 2, "is_admin": 0, "name": "普通用户", "key": "public",
             "menu": [],
             "permission": []
             },

        ]
        self.save(Role, data, "角色表")

    def init_users(self):
        """
        初始化用户表
        """
        data = [
            {"id": "1", "is_superuser": 1, "is_staff": 1, "avatar": "avatar.gif",
             "username": "admin", "name": "admin", "password": "pbkdf2_sha256$216000$NwizGcvlipx7$gra+iEew3CwQDAxyKnPHe8f/em2f55ale+ov2QPaAXE=",
             },
            {"id": "2",  "is_staff": 1, "avatar": "avatar.gif",
             "username": "tps2021", "name": "管理员", "password": "pbkdf2_sha256$216000$OLMzGWFCvQiH$rQMcM7sgvi36iqJmnZSxnnVpWDNrkgCWGMfe681qI+0=",
             "dept_id": "1", "dept_belong_id": "1", "role": ["1"],
             },

        ]
        self.save(UserProfile, data, "用户表", no_reset=True)

    def run(self):
        self.init_dept()
        self.init_button()
        self.init_menu()
        self.init_menu_button()
        self.init_role()
        self.init_users()


# 项目init 初始化，默认会执行 main 方法进行初始化
def main(reset=False):
    Initialize(reset).run()
    pass


if __name__ == '__main__':
    main()
